package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/nursingLevel")
@Api(tags = "护理等级管理")
public class NursingLevelController {

    @Autowired
    private NursingLevelService nursingLevelService;

    /**
     * 查询所有护理等级信息
     * @return
     */
    @GetMapping("/listAll")
    @ApiOperation(value = "查询所有护理等级信息")
    public ResponseResult<List<NursingLevelVo>> listAll() {
//        List<NursingLevelVo> nursingLevelDtos = nursingLevelService.listAll();
        List<NursingLevelVo> nursingLevelDtos = BeanUtil.copyToList(nursingLevelService.list(),NursingLevelVo.class);
        return ResponseResult.success(nursingLevelDtos);
    }

    /**
     * 批量插入护理等级信息
     * @param nursingLevelDtos
     * @return
     */
    @PostMapping("/insertBatch")
    @ApiOperation(value = "批量插入护理等级信息")
    public ResponseResult insertBatch(
            @ApiParam(value = "护理等级数据列表", required = true)
            @RequestBody List<NursingLevelDto> nursingLevelDtos) {
//        nursingLevelService.insertBatch(nursingLevelDtos);
        nursingLevelService.saveBatch(BeanUtil.copyToList(nursingLevelDtos,NursingLevel.class));
        return ResponseResult.success();
    }

    /**
     * 插入护理等级信息
     * @param nursingLevel
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "插入护理等级信息")
    public ResponseResult insert(
            @ApiParam(value = "护理等级数据", required = true)
            @RequestBody NursingLevelDto nursingLevel) {
//        nursingLevelService.insert(nursingLevel);
        nursingLevelService.save(BeanUtil.toBean(nursingLevel,NursingLevel.class));
        return ResponseResult.success();
    }

    /**
     * 更新护理等级信息
     * @param nursingLevel
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新护理等级信息")
    public ResponseResult update(
            @ApiParam(value = "护理等级数据", required = true)
            @RequestBody NursingLevelDto nursingLevel) {
//        nursingLevelService.update(nursingLevel);
        NursingLevel level = BeanUtil.toBean(nursingLevel, NursingLevel.class);
        nursingLevelService.updateById(level);
        return ResponseResult.success();
    }

    /**
     * 删除护理等级信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除护理等级信息")
    public ResponseResult delete(
            @ApiParam(value = "护理等级ID", required = true)
            @PathVariable("id") Long id) {
        nursingLevelService.removeById(id);
        return ResponseResult.success();
    }

    /**
     * 根据ID查询护理等级信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询护理等级信息")
    public ResponseResult<NursingLevelVo> findById(
            @ApiParam(value = "护理等级ID", required = true)
            @PathVariable("id") Long id) {
//        NursingLevelVo nursingLevelVo = nursingLevelService.getById(id);
        NursingLevel nursingLevel = nursingLevelService.getById(id);
        NursingLevelVo nursingLevelVo = BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
        return ResponseResult.success(nursingLevelVo);
    }

    /**
     * 分页查询护理等级信息
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @GetMapping("/listByPage")
    @ApiOperation(value = "分页查询护理等级信息")
    public ResponseResult<PageResponse<NursingLevelVo>> listByPage(
            @ApiParam(value = "页码", required = true)
            @RequestParam("pageNum") Integer pageNum,
            @ApiParam(value = "每页大小", required = true)
            @RequestParam("pageSize") Integer pageSize,
            @ApiParam(value = "护理等级名称")
            @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "护理等级状态")
            @RequestParam(value = "status", required = false) Integer status) {
        PageResponse<NursingLevelVo> pageResponse = nursingLevelService.listByPage(pageNum, pageSize, name, status);
//        Page<NursingLevel> page = new Page<>(pageNum,pageSize);
//        LambdaQueryWrapper<NursingLevel> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(NursingLevel::getName,name);
//        lqw.eq(NursingLevel::getStatus,status);
//        PageResponse<Serializable> pageResponse = PageResponse.of(nursingLevelService.page(page, lqw), NursingLevelVo.class);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 启用/禁用护理等级
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/status/{status}")
    @ApiOperation("启用/禁用护理等级")
    public ResponseResult enableOrDisable(
            @ApiParam(value = "护理等级ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "状态，0：禁用，1：启用", required = true)
            @PathVariable Integer status) {
//        nursingLevelService.enableOrDisable(id, status);
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setId(id);
        nursingLevel.setStatus(status);
        nursingLevelService.updateById(nursingLevel);
        return ResponseResult.success();
    }
}
